using System.Collections.Generic;
using System.Threading.Tasks;
using JGSY.CMS.LowCode.Platform.Domain.Entities;

namespace JGSY.CMS.LowCode.Platform.Domain.Interfaces
{
    /// <summary>
    /// 产品仓储接口
    /// 提供产品数据访问操作，支持产品管理和库存控制
    /// </summary>
    public interface IProductRepository
    {
        /// <summary>
        /// 获取所有产品记录
        /// </summary>
        /// <returns>产品实体列表</returns>
        Task<IEnumerable<Product>> GetAllAsync();
        
        /// <summary>
        /// 根据ID获取产品记录
        /// </summary>
        /// <param name="id">产品标识符</param>
        /// <returns>产品实体，如果未找到则返回null</returns>
        Task<Product?> GetByIdAsync(long id);

        /// <summary>
        /// 根据SKU获取产品记录
        /// </summary>
        /// <param name="sku">产品SKU</param>
        /// <returns>产品实体，如果未找到则返回null</returns>
        Task<Product?> GetBySkuAsync(string sku);

        /// <summary>
        /// 根据别名获取产品记录
        /// </summary>
        /// <param name="slug">产品别名</param>
        /// <returns>产品实体，如果未找到则返回null</returns>
        Task<Product?> GetBySlugAsync(string slug);

        /// <summary>
        /// 根据分类获取产品列表
        /// </summary>
        /// <param name="categoryId">分类ID</param>
        /// <returns>产品列表</returns>
        Task<IEnumerable<Product>> GetByCategoryAsync(long categoryId);

        /// <summary>
        /// 获取推荐产品
        /// </summary>
        /// <param name="count">数量限制</param>
        /// <returns>推荐产品列表</returns>
        Task<IEnumerable<Product>> GetFeaturedProductsAsync(int count = 10);

        /// <summary>
        /// 获取热销产品
        /// </summary>
        /// <param name="count">数量限制</param>
        /// <returns>热销产品列表</returns>
        Task<IEnumerable<Product>> GetBestSellingProductsAsync(int count = 10);

        /// <summary>
        /// 获取最新产品
        /// </summary>
        /// <param name="count">数量限制</param>
        /// <returns>最新产品列表</returns>
        Task<IEnumerable<Product>> GetLatestProductsAsync(int count = 10);

        /// <summary>
        /// 获取低库存产品
        /// </summary>
        /// <param name="threshold">库存阈值</param>
        /// <returns>低库存产品列表</returns>
        Task<IEnumerable<Product>> GetLowStockProductsAsync(int threshold = 10);

        /// <summary>
        /// 分页获取产品列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="keyword">搜索关键词</param>
        /// <param name="categoryId">分类ID</param>
        /// <param name="status">状态过滤</param>
        /// <param name="minPrice">最小价格</param>
        /// <param name="maxPrice">最大价格</param>
        /// <param name="sortBy">排序字段</param>
        /// <param name="sortDesc">是否降序</param>
        /// <returns>分页结果</returns>
        Task<(IEnumerable<Product> Items, int TotalCount)> GetPagedAsync(
            int pageIndex, 
            int pageSize, 
            string? keyword = null, 
            long? categoryId = null,
            byte? status = null,
            decimal? minPrice = null,
            decimal? maxPrice = null,
            string? sortBy = null,
            bool sortDesc = false);
        
        /// <summary>
        /// 添加新产品
        /// </summary>
        /// <param name="entity">要添加的产品实体</param>
        /// <returns>添加操作任务</returns>
        Task AddAsync(Product entity);
        
        /// <summary>
        /// 更新产品信息
        /// </summary>
        /// <param name="entity">更新后的产品实体</param>
        /// <returns>更新操作任务</returns>
        Task UpdateAsync(Product entity);
        
        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="id">产品标识符</param>
        /// <returns>删除操作任务</returns>
        Task DeleteAsync(long id);

        /// <summary>
        /// 批量删除产品
        /// </summary>
        /// <param name="ids">产品ID列表</param>
        /// <returns>删除数量</returns>
        Task<int> BatchDeleteAsync(IEnumerable<long> ids);

        /// <summary>
        /// 更新产品库存
        /// </summary>
        /// <param name="id">产品ID</param>
        /// <param name="stock">库存数量</param>
        /// <returns>是否更新成功</returns>
        Task<bool> UpdateStockAsync(long id, int stock);

        /// <summary>
        /// 增加产品库存
        /// </summary>
        /// <param name="id">产品ID</param>
        /// <param name="quantity">增加数量</param>
        /// <returns>是否更新成功</returns>
        Task<bool> IncreaseStockAsync(long id, int quantity);

        /// <summary>
        /// 减少产品库存
        /// </summary>
        /// <param name="id">产品ID</param>
        /// <param name="quantity">减少数量</param>
        /// <returns>是否更新成功</returns>
        Task<bool> DecreaseStockAsync(long id, int quantity);

        /// <summary>
        /// 增加产品销量
        /// </summary>
        /// <param name="id">产品ID</param>
        /// <param name="quantity">销售数量</param>
        /// <returns>是否更新成功</returns>
        Task<bool> IncrementSalesCountAsync(long id, int quantity = 1);

        /// <summary>
        /// 检查产品SKU是否存在
        /// </summary>
        /// <param name="sku">产品SKU</param>
        /// <param name="excludeId">排除的产品ID</param>
        /// <returns>是否存在</returns>
        Task<bool> SkuExistsAsync(string sku, long? excludeId = null);

        /// <summary>
        /// 检查产品别名是否存在
        /// </summary>
        /// <param name="slug">产品别名</param>
        /// <param name="excludeId">排除的产品ID</param>
        /// <returns>是否存在</returns>
        Task<bool> SlugExistsAsync(string slug, long? excludeId = null);

        /// <summary>
        /// 搜索产品
        /// </summary>
        /// <param name="keyword">搜索关键词</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>搜索结果</returns>
        Task<(IEnumerable<Product> Items, int TotalCount)> SearchAsync(
            string keyword, 
            int pageIndex = 1, 
            int pageSize = 20);
    }
}
